package com.huluobo.jetpackkotlindemo.hilt

import com.squareup.okhttp.Call
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.Request
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

/**
 * Created by lc on 2022/12/14.
 * @Module 表示这个适用于提供依赖注入的实例的模块
 * @InstallIn 表示要装到哪个模块中
 * ActivityComponent 表示要装载到Activity组件中
 * @Provides 表示当前修饰的方法的返回类型被暴露出去,在外部可以直接使用
 * @Singleton 表示当前修饰的返回类型,是单例模式
 */
@Module
@InstallIn(ApplicationComponent::class)
class NetUtils {
    @QualifierConfig.OkHttpClientCall
    @Provides
    fun getOkHttpClientCall(): Call {
        val okHttpClient = OkHttpClient()
        okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS)
        val request = Request.Builder().url("")
            .build()
        return okHttpClient.newCall(request)
    }

//    @QualifierConfig.OtherOkHttpClientCall
//    @Provides
//    fun getOtherOkHttpClientCall(url: String): Call {
//        val okHttpClient = OkHttpClient()
//        okHttpClient.setConnectTimeout(20, TimeUnit.SECONDS)
//        val request = Request.Builder().url(url)
//            .build()
//        return okHttpClient.newCall(request)
//    }

    @Singleton
    @Provides
    fun getRetrofit(): Retrofit {
        return Retrofit.Builder()
            .baseUrl("")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build()
    }
}